package com.example.xw.common.sentinel.nacos;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import org.springframework.stereotype.Component;

@Component
public class GlobalFallback implements BlockExceptionHandler {
    @Override
    public void handle(javax.servlet.http.HttpServletRequest httpServletRequest
            , javax.servlet.http.HttpServletResponse httpServletResponse, BlockException e) throws Exception {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        if(e instanceof DegradeException) {
            httpServletResponse.getWriter().write("{\"code\":\"6000\",\"msg\":\"服务器暂不可用，请稍后再试\",\"data\":null}");
            return;
        }
        if(e instanceof DegradeException) {
            httpServletResponse.getWriter().write("{\"code\":\"6001\",\"msg\":\"服务器繁忙限流，请稍后再试\",\"data\":null}");
            return;
        }
    }
}
